﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OAuth;

namespace Tests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class Parameter
    {
        const string UNRESERVEDCHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";

        [TestMethod]
        public void TestEncoding()
        {
            Func<string, string> testEncoding = str =>
            {
                UnEncodedURLParameter param = new UnEncodedURLParameter()
                {
                    Value = str
                };

                return param.Encode().Value;
            };

            Func<string, string> testDecoding = str =>
                {
                    EncodedURLParameter param = new EncodedURLParameter()
                    {
                        Value = str
                    };
                    return param.Decode().Value;
                };


            Assert.AreEqual("testword", testDecoding(testEncoding("testword")));
            Assert.AreEqual("TESTWORD", testDecoding(testEncoding("TESTWORD")));
            Assert.AreEqual("123345-_.~", testDecoding(testEncoding("123345-_.~")));
            Assert.AreEqual("\"Aardvarks lurk, OK?\"", testDecoding(testEncoding("\"Aardvarks lurk, OK?\""))); // %22Aardvarks%20lurk%2C%20OK%3F%22
            Assert.AreEqual("wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", testEncoding(testDecoding("wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D")));
        }

        [TestMethod]
        public void TestParameterRead()
        {
            var parameterReaderParseParameters = new List<EncodedURLParameter>(
                ParameterReader.ParseParameters("oauth_token=ab3cd9j4ks73hf7g&oauth_token_secret=xyz4992k83j47x0b"));
            Assert.IsTrue(
                parameterReaderParseParameters
                    .Contains(new EncodedURLParameter()
                    {
                        Key = "oauth_token",
                        Value = "ab3cd9j4ks73hf7g"
                    }));
            Assert.IsTrue(
                parameterReaderParseParameters
                    .Contains(new EncodedURLParameter()
                    {
                        Key = "oauth_token_secret",
                        Value = "xyz4992k83j47x0b"
                    }));

        }

    }
}
